package com.hgk.wanwan.main.fragment.pushfragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.android.volley.VolleyError;
import com.hgk.common.common.ui.activity.ImagePreviewActivity;
import com.hgk.common.common.ui.activity.PicSelectActivity;
import com.hgk.common.common.ui.fragment.SimpleBaseFragment;
import com.hgk.common.common.ui.model.PhotosBean;
import com.hgk.common.common.ui.view.BallGridView;
import com.hgk.common.common.ui.view.RippleView;
import com.hgk.wanwan.main.fragment.pushfragment.adapter.PushGridAdapter;
import com.hgk.wanwan.main.fragment.pushfragment.viewui.BaiduMapActivity;

import java.util.ArrayList;
import java.util.Map;

import width.hgk.wanwan.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class PushFragment extends SimpleBaseFragment {

    private BallGridView mBaGridView;//主题图片设置
    private PushGridAdapter mGridAdapter;
    private ArrayList<PhotosBean> mPhotos;
    private ArrayList<String> mPhths;
    private int picNum = 9;
    private PhotosBean mAddBean ;
    private EditText mTitleName;//活动名称
    private EditText mAddCode;//活动标签
    private EditText mLoation;//活动地点
    private EditText mPeopleNum;//人数限制
    private EditText mCostForAc;//费用包括项目
    private EditText mCostNot;//不包括的费用
    private EditText mOtherInst;//其他活动须知
    private TextView mAddDetail;//添加活动详情
    private TextView mReceipt;//收款方式
    private TextView mTimeBucket;//时间段
    private TextView mTimeGone;//结束时间
    private TextView mAcStye;//活动类别
    private RippleView mPreviewBtn;//预览
    private RippleView mPushBtn;//发布
    private LinearLayout mLltLoacation ;
    private EditText mEvLoation;
    //title
    private LinearLayout mTitleLayout;
    private TextView mTitleView;
    private Button btnTitleLeft,btnTitltRight;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);

    }

    private void initView(View view) {
        //TODO 初始化界面控件
        mTitleLayout = (LinearLayout) view.findViewById(R.id.header_pushfrag_view);
        initTitleView(mTitleLayout);
        initGridView(view);
        initTextView(view);
    }

    private void initTitleView(LinearLayout view) {
        mTitleView = (TextView) view.findViewById(R.id.tv_wherefrag_title);
        mTitleView.setVisibility(View.VISIBLE);
        mTitleView.setText("发布页");
        btnTitleLeft = (Button) view.findViewById(R.id.btn_left_header);
        btnTitltRight = (Button) view.findViewById(R.id.btn_right_header);
        btnTitltRight.setVisibility(View.VISIBLE);
    }

    private void initTextView(View view) {
        mLltLoacation = (LinearLayout) view.findViewById(R.id.llt_pushfragment_location);
        mEvLoation = (EditText) view.findViewById(R.id.er_push_act_loation);
        mLltLoacation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaiduMapActivity.launch(mContext);
            }
        });
    }

    //活动主题相片
    private void initGridView(View view) {
        mBaGridView = (BallGridView) view.findViewById(R.id.balgv_title_pic);
        mGridAdapter = new PushGridAdapter(mContext,mListener);
        mAddBean = new PhotosBean();
        mAddBean.setPic_path("-1");
        mPhotos = new ArrayList<PhotosBean>();
        mPhths = new ArrayList<String>();
        mPhotos.add(mAddBean);
        mGridAdapter.setData(mPhotos);
        mBaGridView.setAdapter(mGridAdapter);
        mBaGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PhotosBean bean = (PhotosBean) parent.getAdapter().getItem(position);
                if (bean != null && !TextUtils.isEmpty(bean.getPic_path())) {
                    if (bean.getPic_path().equals("-1")) {
                        //TODO 跳转到照片显示界面
                        PicSelectActivity.launch(mContext, picNum, true, PicSelectActivity.PUBLISH_PLAYING);
                    } else {
                        //TODO 跳转到图片详情界面
                        if (mPhotos.contains(mAddBean)) {//显示详情时，不能够显示默认图片（去除）
                            mPhotos.remove(mAddBean);
                        }
                        ImagePreviewActivity.launch(mContext, position, null, mPhotos, true);
                    }
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected Map getApiParams() {
        return null;
    }

    @Override
    protected void success(String action, Object result) {

    }

    @Override
    protected void fail(String action, String errorStr) {

    }

    @Override
    protected void netfail(String action, VolleyError volleyError) {

    }

    @Override
    public void setLeftButton(RelativeLayout relativeLayout, Button button) {

    }

    @Override
    public void setRightButton(RelativeLayout relativeLayout, Button button) {

    }

    @Override
    public void setTitleView(LinearLayout linearlayout) {

    }

    private PushGridAdapter.DelePicIconListener  mListener = new PushGridAdapter.DelePicIconListener() {
        @Override
        public void OnCliclike(PhotosBean data, PushGridAdapter.ViewHolder vh) {
            //删除图片
            mPhotos.remove(data);
            mPhotos.add(mAddBean);
            mGridAdapter.clear();
            mGridAdapter.setData(mPhotos);
            mBaGridView.setAdapter(mGridAdapter);
            mGridAdapter.notifyDataSetChanged();
        }
    };
    @Override
    public Object getTitleString() {
        return "发布活动页";
    }

    @Override
    public int getBodyView() {
        return R.layout.fragment_push;
    }
}
